package com.tzpay.tzpay.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tzpay.tzpay.R;
import com.tzpay.tzpay.bean.FunctionInfoWrapper;
import com.tzpay.tzpay.bean.OrderInfo;
import com.tzpay.tzpay.common.ConstValue;
import com.tzpay.tzpay.ui.activity.TradeDetailActivity;
import com.tzpay.tzpay.utils.Utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by chenfeiyue on 2016/3/13 0013.
 */
public class TradeListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<OrderInfo> infos;
    private Context context;

    public TradeListAdapter(Context context, ArrayList<OrderInfo> infos) {
        this.infos = infos;
        this.context = context;
    }

    public void addData(ArrayList<OrderInfo> infos) {
        this.infos.addAll(infos);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.infos.clear();
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(
                context).inflate(R.layout.item_trade_list, parent,
                false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final OrderInfo info = infos.get(position);
        if (holder instanceof MyViewHolder) {
            fillData((MyViewHolder) holder, info);
        }
    }


    private void fillData(MyViewHolder holder, final OrderInfo info) {
        String amount = info.getOrderAmt();
        if (!TextUtils.isEmpty(amount)) {
            holder.tv_amount.setText(context.getString(R.string.text_rmb) + Utils.getDotNumber(amount));
        }
        holder.tv_status.setText(info.getTradeStatus());
        holder.tv_no.setText(info.getOrderNo());
        holder.tv_time.setText(Utils.formatTime(info.getOrderDate()));

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, TradeDetailActivity.class);
                intent.putExtra("order_info", info);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return infos.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_amount)
        TextView tv_amount;
        @Bind(R.id.tv_status)
        TextView tv_status;
        @Bind(R.id.tv_no)
        TextView tv_no;
        @Bind(R.id.tv_time)
        TextView tv_time;

        @Bind(R.id.view)
        View view;

        public MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
